package cn.pn.project.controller;
import cn.pn.project.client.base.CouponFeign;
import cn.pn.project.client.commodity.CommodityFeign;
import cn.pn.project.client.user.UserCouponFeign;
import cn.pn.project.client.user.UserFeign;
import cn.pn.project.common.RedisUtils;
import cn.pn.project.pojo.base.Coupon;
import cn.pn.project.pojo.commodity.Commodity;
import cn.pn.project.pojo.user.User;
import cn.pn.project.pojo.user.Usercoupon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("Coupon")
public class CouponController {
    @Autowired
    private CouponFeign couponFeign;
    @Autowired
    private UserCouponFeign userCouponFeign;
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private CommodityFeign commodityFeign;
    /**
     *  根据状态和用户id查询优惠券id
     * @param status   状态
     * @return  优惠券id集合
     */
    @PostMapping("/v/findByStatus")
    @ResponseBody
    List<Coupon> findByStatus(@RequestParam("status") Integer status,HttpServletRequest request) throws Exception {

        //从redis中获取user
        String token=request.getHeader("token");
        User user=(User)redisUtils.get(token);
        //调用用户优惠券表的方法查询到某个用户的某个状态的优惠券
        List<Usercoupon> usercouponList=userCouponFeign.findByStatus(status,user.getUserId());
        System.out.println("**************************************"+usercouponList.size());
        //创建优惠券表的集合
        List<Coupon> couponList=new ArrayList<Coupon>();
        //循环得到的用户优惠券表中的优惠券id获取优惠券的详情
        for (Usercoupon usercoupon: usercouponList
        ) {
            Coupon coupon = couponFeign.findByCouponId(usercoupon.getCouponId());
            couponList.add(coupon);
            System.out.println(coupon.getCouponName());
        }
        return couponList;
    }

    /**
     * 消耗积分领取优惠券   还有死数据
     * @param couponId 优惠券id
     * @param
     * @return
     */
    @RequestMapping("/v/addUsercoupon")
    @ResponseBody
    Integer addUsercoupon(@RequestParam("couponId") Integer couponId,  HttpServletRequest request) throws Exception{
        Integer couent=0;
        boolean bool=false;

        //从redis中获取user
        String token=request.getHeader("token");
        User usertoo=(User)redisUtils.get(token);

        User user=userFeign.findUserById(1);
        System.out.println(user.getIntegral()+"积分++++++++++++++++++++++");
        //判断用户积分是否大于等于20,满足抽奖要求
        if(user.getIntegral()>=20){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            //拿到优惠券信息
            Coupon coupon = couponFeign.findByCouponId(couponId);
            //创建用户优惠券
            Usercoupon usercoupon=new Usercoupon();
            //判断优惠券类型
            if(coupon.getNumberDays()==0){
                usercoupon.setGetDate(new Date());//传入开始时间
                usercoupon.setLoseDate(coupon.getEndTime());//传入结束时间
            }else{
                usercoupon.setGetDate(new Date());//传入开始时间
                Calendar rightNow = Calendar.getInstance();
                rightNow.add(Calendar.DATE,+coupon.getNumberDays());//当前时间+天数=结束日期
                usercoupon.setLoseDate(rightNow.getTime());//传入结束时间
            }
            //将信息装入实体类
            usercoupon.setUserId(1);
            usercoupon.setCouponId(couponId);
            //满足要求进行抽奖,并且减去积分20
            user.setIntegral(user.getIntegral()-20);







            //修改用户的积分 -20
            bool=userFeign.updateUserIntegral(user);
            /*redisUtils.update(token,user);*/
           //判断是否减去积分
            System.out.println(bool);
            if(bool){//成功减去积分
                //执行添加操作
                couent=userCouponFeign.AddUsercoupon(usercoupon);
            }
        }
        return couent;
    }

    /**
     * 免费领取
     * @param couponId 优惠券id
     * @param
     * @return
     */
    @RequestMapping("/v/addUsercouponmf")
    @ResponseBody
    Integer addUsercouponmf(@RequestParam("couponId") Integer couponId, HttpServletRequest request) throws Exception {
        String token=request.getHeader("token");
        User user=(User)redisUtils.get(token);
        Integer couent=0;
        //创建用户优惠券
        Usercoupon usercoupon=new Usercoupon();
        usercoupon.setUserId(user.getUserId());
        usercoupon.setCouponId(couponId);
        //拿到优惠券信息
        Coupon coupon = couponFeign.findByCouponId(couponId);
        if(coupon.getNumberDays()==0){
            usercoupon.setGetDate(new Date());//传入开始时间
            usercoupon.setLoseDate(coupon.getEndTime());//传入结束时间
        }else{
            System.out.println("++++++++++++++++++++++++-------else"+coupon.getNumberDays());
            usercoupon.setGetDate(new Date());//传入开始时间
            Calendar rightNow = Calendar.getInstance();
            rightNow.add(Calendar.DATE,+coupon.getNumberDays());//当前时间+天数=结束日期
            usercoupon.setLoseDate(rightNow.getTime());//传入结束时间
        }
        //执行添加操作
        couent=userCouponFeign.AddUsercoupon(usercoupon);
        return couent;
    }

    /**
     * 使用优惠券显示
     * @param
     * @param commodityId    商品id
     * @return
     */

    @ResponseBody
    @RequestMapping("/v/findCouponOrder")
    List<Coupon> findCouponOrder(HttpServletRequest request, @RequestParam("commodityId") Integer commodityId) throws Exception {
        //先在redis中获取到用户的id

        String token=request.getHeader("token");
        User user=(User)redisUtils.get(token);

        //调用查用户优惠券的方法  状态和用户id
        List<Usercoupon> usercouponList = userCouponFeign.findByStatus(0,user.getUserId());

        //通过商品id获取到商品的所有信息
        Commodity commodity= commodityFeign.findCommodityAllMessage(commodityId);
        //获取到该商品以及类型的优惠券
        List<Coupon> couponList = couponFeign.findAllByCouponTypeAndReleId(commodity.getClildTypeId(),commodity.getCommodityId());
        //最终存值的集合
        List<Coupon> coupons =new ArrayList<Coupon>();

        for(int i=0;i<couponList.size();i++){
            for (int j=0;j<usercouponList.size();j++){
                if(couponList.get(i).getCouponId()==usercouponList.get(j).getCouponId()){
                    coupons.add(couponList.get(i));
                }
            }
        }
        System.out.println(coupons.size()+"55555555555555555555555555555555555555555555555");
        return coupons;
    }

}
